//
//  TSPictureColletionView.swift
//  Picture
//
//  Created by 王磊 on 28/04/2017.
//  Copyright © 2017 ThreeStone. All rights reserved.
//

import UIKit

extension TSPictureColletionView {
    
    override static func getInstance() -> TSPictureColletionView {
        
        let flowLayout = TSPictureFlowLayout()
        
        return TSPictureColletionView.getInstance(flowLayout)
    }
    static func getInstance(flowLayout: UICollectionViewFlowLayout) -> TSPictureColletionView {
        
        return TSPictureColletionView(frame: CGRectZero, collectionViewLayout: flowLayout)
    }
}

class TSPictureColletionView: UICollectionView {
    
    private var dataArray: Array<AnyObject> = []
    
    internal weak var pictureDelegate: TSPictureCollectionViewDelegate?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        commitInitOwnProperties()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
extension TSPictureColletionView {
    
}

extension TSPictureColletionView {
    
    internal func updateData(arr: Array<AnyObject>) {
        
        dataArray += arr
        
        reloadData()
    }
}

private let cellIdentifier: String = "TSPictureCollectionViewCell"
extension TSPictureColletionView {
    
    internal func commitInitOwnProperties() {
        
        showsVerticalScrollIndicator = false
        
        showsHorizontalScrollIndicator = false
        
        bounces = true
        
        dataSource = self
        
        delegate = self
        
        registerClass(TSPictureCollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        
        clipsToBounds = false
        
        backgroundColor = UIColor.clearColor()
    }
}

// MARK: data source
extension TSPictureColletionView: UICollectionViewDataSource {}

extension TSPictureColletionView {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! TSPictureCollectionViewCell
        
        cell.indexPath = indexPath
        
        cell.updateData(dataArray[indexPath.row])
        
        cell.addTarget(self, action: #selector(cellAction), forEvent: TSItemClickType.Comment)
        
        return cell
    }
}

extension TSPictureColletionView {
    
    @objc private func cellAction(temp: TSArguInfo) {
        
        performSelWithArgu(temp)
        
    }
}

// MARK: delegate
extension TSPictureColletionView: UICollectionViewDelegate {}

extension TSPictureColletionView {
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        if unsureObervers != nil {
            
            let argu = TSArguInfo()
            
            argu.data = dataArray[indexPath.item]
            
            argu.view = self
            
            argu.indexPath = indexPath
            
            argu.itemType = TSItemClickType.PictureItem
            
            performSelWithArgu(argu)
            
        }  else {
            
            guard let mDelegate = pictureDelegate else {
                
                return
            }
            
            guard mDelegate.respondsToSelector(#selector(mDelegate.onPictureCollectionItemSelected(_:indexPath:))) else {
                
                return
            }
            
            mDelegate.onPictureCollectionItemSelected(dataArray[indexPath.item], indexPath: indexPath)
        }
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeMake(Picture_Item_Width, Picture_Item_Height)
    }
}



